package rentcar.web;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;









import rentcar.domain.Car;
import rentcar.domain.UserPrincipal;
import rentcar.domain.accessRight;
import rentcar.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listUsers() {

		ModelAndView mav = new ModelAndView();
		mav.addObject("users", userService.getUsers());
		mav.setViewName("user.list");
		return mav;
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String showUserProfile(
			@PathVariable("id") UserPrincipal user,
			HttpSession session, Model model) {

		model.addAttribute("user", user);
		
		return "user.details";
	}		


	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ModelAndView newUserForm(Model model) {
		
		populateControls(model);
		model.addAttribute("user", new UserPrincipal(0L, "Логин", "Пароль", "ФИО", "Е-мэйл", "№ паспорта", "Права доступа"));
		model.addAttribute("action", "new");

		ModelAndView mav = new ModelAndView();
		mav.setViewName("user.form");
		return mav;
	}	
	
	
	
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String newUserSubmit(
			@ModelAttribute UserPrincipal user, 
			Model model) {
		
		if (user != null) {
			userService.saveUser(user);			
			model.addAttribute("users", userService.getUsers());
		}
		model.asMap().remove("user");
		return "user.list";
	}	
	


	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editUserForm(
			@PathVariable("id") UserPrincipal user,
			HttpSession session, Model model) {

		populateControls(model);		
		
		model.addAttribute("user", user);
		return "user.form.edit";
	}	

	
	private void populateControls(Model model) {
		
	
		Map<String, String> accessList = new LinkedHashMap<String, String>();
				
		for (accessRight ar : accessRight.values())	{
		   //accessList.put( Integer.toString(ar.ordinal()), ar.toString());
		   accessList.put( ar.toString(), ar.toString());	
		}
		model.addAttribute("accessList", accessList);

	}
	
	
	
	
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String editUserSubmit(
			@ModelAttribute UserPrincipal user, 
			Model model) {
		
		if (user != null) {
			//System.out.println("active:" + client.isActive());
			userService.updateUser(user);			
			model.addAttribute("users", userService.getUsers());
		}
		
		model.asMap().remove("user");
		return "user.list";
	}


	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteUser(
			@PathVariable("id") UserPrincipal user,
			HttpSession session, Model model) {
		
		userService.deleteUser(user);			
		model.addAttribute("users", userService.getUsers());
		
		return "user.list";
	}
	
	
	
	
}
